package cn.org.rookie.jeesdp.mapper.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    public static final Pattern humpPattern = Pattern.compile("[A-Z]");
    public static final Pattern underlinePattern = Pattern.compile("_(\\w)");

    /**
     * 驼峰转下划线
     *
     * @param str 要转换的字符串
     * @return 转换后的字符串
     */

    public static String humpToUnderline(String str) {
        Matcher matcher = humpPattern.matcher(str.substring(0, 1).toLowerCase() + str.substring(1));
        StringBuilder sb = new StringBuilder();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 下划线转驼峰
     *
     * @param str 要转换的字符串
     * @return 转换后的字符串
     */

    public static String underlineToHump(String str) {
        Matcher matcher = underlinePattern.matcher(str);
        StringBuilder sb = new StringBuilder();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
